﻿using System;
using System.Collections.Generic;
using System.Text;
using POSPlus.BTxmlWriter;
using System.Data;
namespace POSPlus.EZBarTender
{
    /// <summary>
    /// Aids in creating the XML file that BarTender is able to use to print labels.
    /// </summary>
    public class BarTenderTreeBuilder : TreeBuilder
    {
        public BarTenderTreeBuilder(string xmlLocation, string textStart) : base(xmlLocation) { }
        public BarTenderTreeBuilder(string xmlLocation, bool printPreview) : base(xmlLocation, printPreview) { }

        /// <summary>
        /// Sets the computer location of the label.
        /// </summary>
        /// <param name="location"></param>
        public void SetFormatLocation(String location) 
        {
            LabelLocation = location;
        }

        /// <summary>
        /// Formats and fills the TextData for the XML Document using the given DataTable.
        /// </summary>
        /// <param name="table"></param>
        public void FillTextData(DataTable table)
        {
            AppendTextData("<![CDATA[");
            String fields = "";
            foreach (DataColumn col in table.Columns)
            {
                fields += "\"" + col.ColumnName + "\"" + ",";
            }
            fields = fields.Substring(0, fields.Length - 1);
            fields += "\n";
            AppendTextData(fields);

            for (int i = 0; i < table.Rows.Count; i++)
            {
                String data = "";
                for (int j = 0; j < table.Rows[i].ItemArray.Length; j++)
                    data += "\"" + table.Rows[i][j].ToString() + "\"" + ",";
                data = data.Substring(0, data.Length - 1);
                if (i + 1 < table.Rows.Count)
                    data += "\n";
                Console.WriteLine(data);
                AppendTextData(data);
            }
        }

		public void StartTextData()
		{
			AppendTextData("<![CDATA[");
		}

        /// <summary>
        /// Finishes the TextData with the correct ending ']]>'.
        /// </summary>
        public void CloseTextData()
        {
            AppendTextData("]]>");
        }
	}
}
